<template>
  <div class="post-comment">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="优质评论将会被优先展示"
      show-word-limit
    />
    <van-button
      class="pub-btn"
      type="primary"
      size="small"
      @click="pub"
      :disabled="!message.length"
      >发布</van-button
    >
  </div>
</template>

<script>
import { addComment } from '@/api/article/comment.js'
export default {
  name: 'CommentPost',
  components: {},
  props: {
    artID: {
      type: [String, Number, Object],
      required: true
    },
    isCommentReply: {
      type: Boolean,
      default: false
    },
    articleID: {
      type: [String, Number, Object]
    }
  },
  data() {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async pub() {
      try {
        const data = {
          target: this.artID,
          content: this.message
        }
        if (this.isCommentReply) {
          data.art_id = this.articleID
        }
        const { data: res } = await addComment(data)
        // 发布文章评论后触发事件，调用父组件方法，将返回的评论对象传递过去
        this.$emit('pubComment', res.data.new_obj)
        this.message = ''
      } catch (error) {}
    }
  }
}
</script>

<style scoped lang="less">
.post-comment {
  display: flex;
  padding: 15px;
  align-items: center;
  .post-field {
    background: #f5f7f9;
    margin-right: 15px;
    flex: 8;
  }
  .pub-btn {
    flex: 2;
  }
}
</style>
